package com.example.thread.demo.dead;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName Solution.java
 * @Description 死锁示例
 * @createTime 2020年11月10日 00:16:00
 */
public class DeadSolution {

    private Object a = new Object();
    private Object b = new Object();

    class PrintA implements Runnable {
        @Override
        public void run() {
            synchronized (a) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b) {
                    System.out.println("printA");
                }
            }
        }
    }

    class PrintB implements Runnable {
        @Override
        public void run() {
            synchronized (b) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a) {
                    System.out.println("printB");
                }
            }
        }
    }

    public static void main(String[] args) {
        DeadSolution solution = new DeadSolution();
        new Thread(solution.new PrintA()).start();
        new Thread(solution.new PrintB()).start();
    }
}
